<?php


namespace app\xiangji\controller;


use app\common\helpers\Response;
use app\Request;
use app\xiangji\BaseController;
use app\xiangji\services\UserService;
use think\App;
use think\exception\ValidateException;

class User extends BaseController
{
    private $userService;

    public function __construct(UserService $userService)
    {
        $this->userService = $userService;
    }

    /**
     * @todo 修改用户手机号
     * @param Request $request
     * @return \think\response\Json
     * @throws \GuzzleHttp\Exception\InvalidArgumentException
     * @throws \think\Exception
     */
    public function modifyPhone(Request $request)
    {
        $result = $this->userService->modifyPhone($request);
        return $this->success($result);
    }

    /**
     * @todo 修改用户信息
     * @param Request $request
     * @return \think\response\Json
     */
    public function modifyUserInfo(Request $request)
    {
        try {
            $this->validate(
                $request->put(['nickName', 'gender', 'avatarUrl']),
                [
                    'nickName'  => 'length:1,25',
                    'gender'    => 'integer|in:1,0',
                    'avatarUrl' => 'url'
                ]
            );
            $result = $this->userService->modifyUserInfo($request);
            return $this->success($result);
        } catch (ValidateException $exception){
            return $this->failed($exception->getMessage(), Response::HTTP_BAD_REQUEST);
        }
    }

    /**
     * @todo 获取分享恋人绑定
     * @return \think\response\Json
     */
    public function getLoverBindToken()
    {
        $result = $this->userService->generateLoverBindToken();
        return $this->success($result);
    }

    /**
     * @todo 通过邀请token绑定恋人关系
     * @param Request $request
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function bindLoverByToken(Request $request)
    {
        $result = $this->userService->bindLoverByToken($request->post('token'));
        return $this->success($result);
    }

    /**
     * @todo 解绑恋人关系
     * @return \think\response\Json
     * @throws \think\Exception
     */
    public function unbindLover()
    {
        $result = $this->userService->unbindLover();
        return $this->success($result);
    }

    /**
     * @todo 用户统计数据
     * @return \think\response\Json
     */
    public function statistics()
    {
        $result = $this->userService->userStatistics();
        return $this->success($result);
    }
}
